/*
 * @Description: app Mutations
 * @Author: chenju
 * @Date: 2021-06-01 11:10:14
 * @LastEditors: qingyang
 * @LastEditTime: 2023-06-29 11:10:08
 */
import type { MutationTree } from 'vuex';
import type { AppState } from './state';
import { AppMutationTypes } from './mutation-types';
import type { IAuditRemind } from '@/apis/common/model/commonModel';

export type Mutations<S = AppState> = {
  [AppMutationTypes.SHOW_LOADING](state: S): void;
  [AppMutationTypes.HIDE_LOADING](state: S): void;
  [AppMutationTypes.SET_KEEP_ALIVE](state: S, routes: string | string[]): void;
  [AppMutationTypes.REMOVE_KEEP_ALIVE](state: S, routes: string | string[]): void;
  [AppMutationTypes.CLEAR_KEEP_ALIVE](state: S): void;
  [AppMutationTypes.SET_MESSAGE_UNREAD_COUNT](state: S, count: number): void;
  [AppMutationTypes.SET_MESSAGE_UNREAD_COUNT_SYS](state: S, count: number): void;
  [AppMutationTypes.SET_MESSAGE_UNREAD_COUNT_WARNING](state: S, count: number): void;
  [AppMutationTypes.TOGGLE_COLLAPSED](state: S): void;
  [AppMutationTypes.SET_COLLAPSED](state: S, bool: boolean): void;
  [AppMutationTypes.SET_LANGUAGE](state: S, language: 'zh' | 'en'): void;
  [AppMutationTypes.SET_TEMP_TASK_NAME](state: S, tempTaskName: string): void;
  [AppMutationTypes.SET_STAGE_ERROR](state: S, stageError: any): void;
  [AppMutationTypes.SET_AUDIT_COUNT](state: S, res: IAuditRemind): void;
  [AppMutationTypes.SET_IS_SUPPLIER](state: S, flag: boolean): void;
};

export const mutations: MutationTree<AppState> & Mutations = {
  [AppMutationTypes.SET_AUDIT_COUNT](state: AppState, res: IAuditRemind) {
    state.dataRequestCount = res.dataRequestCount || 0;
    state.businessOppoCount = res.businessOppoCount || 0;
    state.opinionCount = res.opinionCount || 0;
    state.auditCount = res.factorCount || 0;
  },
  [AppMutationTypes.SET_STAGE_ERROR](state: AppState, stageError: any) {
    state.stageError = stageError;
  },
  [AppMutationTypes.SHOW_LOADING](state: AppState) {
    state.spinning = true;
  },
  [AppMutationTypes.HIDE_LOADING](state: AppState) {
    state.spinning = false;
  },
  [AppMutationTypes.SET_KEEP_ALIVE](state: AppState, routes: string | string[]) {
    if (typeof routes === 'string') {
      const index = state.keepAliveRoutes.indexOf(routes);
      if (index < 0) state.keepAliveRoutes.splice(index, 0, routes);
    } else {
      for (const i in routes) {
        const route = routes[i];
        const index = state.keepAliveRoutes.indexOf(route);
        if (index < 0) state.keepAliveRoutes.splice(index, 1, route);
      }
    }
  },
  [AppMutationTypes.REMOVE_KEEP_ALIVE](state: AppState, routes: string | string[]) {
    if (typeof routes === 'string') {
      const index = state.keepAliveRoutes.indexOf(routes);
      if (index > -1) state.keepAliveRoutes.splice(index, 1);
    } else {
      for (const i in routes) {
        const route = routes[i];
        const index = state.keepAliveRoutes.indexOf(route);
        if (index > -1) state.keepAliveRoutes.splice(index, 1);
      }
    }
  },
  [AppMutationTypes.CLEAR_KEEP_ALIVE](state: AppState) {
    state.keepAliveRoutes = [];
  },
  // 设置未读消息数量
  [AppMutationTypes.SET_MESSAGE_UNREAD_COUNT](state: AppState, count: number) {
    state.messageUnreadCount = count;
  },
  // 设置未读消息数量 系统消息（通知消息）
  [AppMutationTypes.SET_MESSAGE_UNREAD_COUNT_SYS](state: AppState, count: number) {
    state.messageUnreadCountSys = count;
  },
  // 设置未读消息数量 预警消息
  [AppMutationTypes.SET_MESSAGE_UNREAD_COUNT_WARNING](state: AppState, count: number) {
    state.messageUnreadCountWarning = count;
  },
  // 侧边栏收起展开
  [AppMutationTypes.TOGGLE_COLLAPSED](state: AppState) {
    state.collapsed = !state.collapsed;
  },
  // 设置侧边栏收起展开
  [AppMutationTypes.SET_COLLAPSED](state: AppState, bool: boolean) {
    state.collapsed = bool;
  },
  [AppMutationTypes.SET_LANGUAGE](state: AppState, language: 'zh' | 'en') {
    state.language = language;
  },
  // 设置任务名称
  [AppMutationTypes.SET_TEMP_TASK_NAME](state: AppState, tempTaskName: string) {
    state.tempTaskName = tempTaskName;
  },
  // 标记是否移除keepAlive
  [AppMutationTypes.SET_REMOVE_ALIVE_FLAG](state: AppState, flag: boolean) {
    state.removeAliveFlag = flag;
  },
  // 设置暂存数据
  [AppMutationTypes.SET_TEMP_DATA_STATIC_BREADCRUMB_NAME](state: AppState, name: string[]) {
    state.tempDataStaticBreadcrumbName = name;
  },
  // 是否是供应商端
  [AppMutationTypes.SET_IS_SUPPLIER](state: AppState, flag: boolean) {
    state.isSupplier = flag || false;
  }
};
